package com.itheima.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.user.UmsMenuService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.dto.UmsMenuDto;
import com.itheima.vo.pojo.UmsMenu;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bouncycastle.jcajce.provider.digest.MD2;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @DubboReference
    private UmsMenuService umsMenuService;

    /*
    * 添加后台菜单
    * */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody UmsMenu umsMenu){
        umsMenu.setCreateTime(LocalDateTime.now());
        umsMenuService.save(umsMenu);
        return AjaxResult.success("操作成功");
    }

    /*
    * 根据ID删除后台菜单
    * */
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        umsMenuService.removeById(id);
        return AjaxResult.success("操作成功");
    }

/*
* 分页查询后台菜单
* */
    @GetMapping("/list/{parentId}")
    public  AjaxResult list (@PathVariable Integer parentId,Integer pageNum,Integer pageSize){
        TableDataInfo tableDataInfo=umsMenuService.queryPage(pageNum,pageSize,parentId);
        return  AjaxResult.success(tableDataInfo);
    }

    /*
    * 修改后台菜单
    * */
    @PostMapping("/update/{id}")
    public AjaxResult update(@PathVariable Long id,@RequestBody UmsMenu umsMenu){
        umsMenu.setId(id);
        umsMenuService.updateById(umsMenu);
        return AjaxResult.success("操作成功");
    }

    /*
    * 修改菜单显示状态
    * */
    @PostMapping("/updateHidden/{id}")
    public AjaxResult updateHidden(@PathVariable Long id,  Long hidden){
        UmsMenu umsMenu = new UmsMenu();
        umsMenu.setHidden(hidden);
        umsMenu.setId(id);
        umsMenuService.updateHidden(umsMenu);
        return AjaxResult.success("操作成功");
    }

/*
树形结构返回所有菜单列表
 */
 @GetMapping("/treeList")
    public  AjaxResult   treeList (){
     List<UmsMenuDto> result = umsMenuService.treeList();

     return AjaxResult.success(result);
    };



    /*
    * 根据ID获取菜单详情
    * */
    @GetMapping("/{id}")
    public  AjaxResult getById(@PathVariable Long id){
        UmsMenu umsMenu = umsMenuService.getById(id);
        return AjaxResult.success(umsMenu);
    }
}
